#include "registration.h"
#include "ui_registration.h"

Registration::Registration(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Registration)
{
    ui->setupUi(this);
    //键盘初始化
    ui->username->setFocus();
    keyboard->initFocusWidget(ui->widget);
    keyboard->hide();
    isKeyBoardShow = 0;
    //ui美化
    this->initStyleSheet();
    //close keyboard
    connect(keyboard,&frmKeyBoard::hiding,this,&Registration::hidekeyboard);
}

Registration::~Registration()
{
    delete ui;
}

void Registration::initStyleSheet()
{
    QString qss;

    qss += "QLineEdit{border-width: 1px; border-radius: 10px; border-style: solid; border-color: rgb(98,183,152); }";
    qss += "#password{border-color:rgb(193,150,144);}";
    /*
    qss += "QPushButton:hover{background-color:rgb(229,229,229); color:black;}";
    qss += "QPushButton:pressed,QPushButton:checked{background-color:rgb(0,118,215);}";
    qss += "#pushButton_closeKeyboard{background-color:rgba(0,0,0,0); border:0px}";
    qss += "#pushButton_closeKeyboard:hover{background-color:#b30220;}";
    qss += "#pushButton_space{min-width:500px;}";
    qss += "#pushButton_backspace,#pushButton_shift{min-width:100px;}";
    qss += "#pushButton_enter{min-width:120px;}";
    qss += "#pushButton_tab,#pushButton_ctrl{min-width:70px;}";
    qss += "#pushButton_capsLock{min-width:80px;}";
    qss += "#pushButton_up{min-width:150px;}";
    */
    qss += "QHeaderView::section {"
                   "background-color: black;"
                   "border: 1px solid gray;"
                   "height: 5px; /* 调整高度 */"
                   "}";
    this->setStyleSheet(qss);
}

void Registration::on_keyboard_clicked()
{
    if(isKeyBoardShow == 0){
        keyboard->setGeometry(0, 175, 431, 166);
        keyboard->show();
        isKeyBoardShow = 1;
    }else{
        keyboard->hide();
        isKeyBoardShow = 0;
    }
}


void Registration::on_confirm_clicked()
{
    QString username = ui->username->text();
    QString password = ui->password->text();
    QString confirmPassword = ui->confirmpassword->text();

    if(password != confirmPassword){
        QMessageBox::warning(this,tr("reg failed"),tr("Please input same password！"),QMessageBox::Ok);
        ui->password->clear();
        ui->confirmpassword->clear();
        ui->password->setFocus();
    }else if(!username.isEmpty()&&!password.isEmpty()){
        UserManager userManager;

        //输入用户命和密码,注册成功则返回true，失败false
        bool registrationResult = userManager.createUser(username, password);

        if (registrationResult) {
            qDebug() << "Registration successful";
        } else {
            qDebug() << "Registration failed";
        }
        keyboard->hide();
        isKeyBoardShow = 0;
        emit display(4);
        emit reset();

    }else{
        QMessageBox::warning(this,tr("reg failed"),tr("can not exist blank！"),QMessageBox::Ok);
    }
}


void Registration::on_cancel_clicked()
{
    keyboard->hide();
    isKeyBoardShow = 0;
    emit display(4);
}


//hide keyboard by keyboard's button
void Registration::hidekeyboard()
{
    keyboard->hide();
    isKeyBoardShow = 0;
}
